# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   ApiTest
# FileName:      read_excel.py
# Author:       liu
# Datetime:     2024/9/7 上午11:32
# Description: 读取excel文件
# ---------------------------------------------------------------------------
import os

import openpyxl

from ApiTest.common.read_basic_ini import Read_basic_ini
from ApiTest.common.read_json import read_json
from ApiTest.common.read_user_ini import Read_user_ini
from ApiTest.config_data.settings import *
from ApiTest.report import log


class Read_excel:
    def __init__(self, username):
        # 获取excel文件
        read_user_ini = Read_user_ini(username=username)
        self.read_basic_ini = Read_basic_ini()
        excel_path = read_user_ini.get_file(FILE_EXCEL)
        self.username = username
        table = read_user_ini.get_table(TABLE)
        # 读取excel
        self.wb = openpyxl.load_workbook(excel_path)
        self.ws = self.wb[table]

    def __get_cell(self, col, row):
        try:
            # 获取单元格数据
            cell_value = self.ws[col + str(row)].value
        except Exception as e:
            log.error(f'获取指定单元格错误，错误为：{e}，传入的列号：{col}， 传入的行号：{row}')
            raise e
        else:
            # 判断是否取空或者出去两边后为空
            if cell_value is None:
                return None
            elif cell_value.strip():
                return cell_value.strip()

    def case_id(self, row):
        # 获取用例编号
        return self.__get_cell(ID, row=row)

    def module_name(self, row):
        # 获取模块名称
        return self.__get_cell(MODULE, row=row)

    def api_name(self, row):
        # 获取接口名称
        return self.__get_cell(API, row=row)

    def title(self, row):
        # 获取标题
        return self.__get_cell(TITLE, row=row)

    def level(self, row):
        # 获取用例等级
        return self.__get_cell(LEVEL, row=row)

    def req_method(self, row):
        # 获取请求方式(post\get\delete\put)，必有
        return self.__get_cell(REQ_METHOD, row=row)

    def url(self, row):
        # 获取url
        host = self.read_basic_ini.get_host(HOST)
        # print(host)
        url = self.__get_cell(URL, row=row)
        return host+url

    def req_type(self, row):
        # 媒体类型
        req_type = self.__get_cell(REQ_TYPE, row=row)
        if req_type:
            return str(req_type).lower()

    def case_data(self, row):
        # 用例数据
        module_name = self.module_name(row=row)
        api_name = self.api_name(row=row)
        case_key = self.__get_cell(CASE_DATA, row=row)
        case_data = read_json(username=self.username, json_name=FILE_CASE)
        return case_data[module_name][api_name][case_key]

    def except_data(self, row):
        # 期望数据
        module_name = self.module_name(row=row)
        api_name = self.api_name(row=row)
        except_key = self.__get_cell(EXCEPT_DATA, row=row)
        except_data = read_json(username=self.username, json_name=FILE_EXCEPT)
        return except_data[module_name][api_name][except_key]

    def sql_type(self, row):
        # sql语句类型
        sql_type = self.__get_cell(SQL_TYPE, row=row)
        if sql_type:
            return str(sql_type).lower()

    def sql_data(self, row):
        # sql数据
        sql_key = self.__get_cell(SQL_DATA, row=row)
        if sql_key:
            module_name = self.module_name(row=row)
            api_name = self.api_name(row=row)
            sql_data = read_json(username=self.username, json_name=FILE_SQL)
            return sql_data[module_name][api_name][sql_key]

    def update_key(self, row):
        # 需要更新的值(指在case_data.json中对应数据需要更新，且写在excel中的key)
        return self.__get_cell(UPDATE_KAY, row=row)

    def get_data(self):
        list_data = []
        for i in range(2, self.ws.max_row + 1):
            case_id = self.case_id(i)
            module_name = self.module_name(i)
            api_name = self.api_name(i)
            title = self.title(i)
            level = self.level(i)
            req_method = self.req_method(i)
            url = self.url(i)
            req_type = self.req_type(i)
            case_data = self.case_data(i)
            except_data = self.except_data(i)
            sql_type = self.sql_type(i)
            sql_data = self.sql_data(i)
            update_key = self.update_key(i)
            if url is not None and except_data is not None and req_method is not None:
                list_data.append([case_id, module_name, api_name, title, level, req_method, url, req_type, case_data, except_data,
                                  sql_type, sql_data, update_key])
        return list_data


if __name__ == '__main__':
    excel = Read_excel('test_demo')
    # print(excel.module_name(2))
    print(excel.get_data())
    # print(excel.url(2))
